package com.johnny.action;

import java.net.Socket;
import java.util.List;

import com.johnny.data.DataPool;
import com.johnny.data.PlayerInfo;
import com.johnny.data.RoomInfo;
import com.johnny.data.TableInfo;
import com.johnny.startup.Casino28App;

import net.sf.json.JSONObject;

public class BetAction implements Action{

	@Override
	public String doAction(JSONObject object, Socket socket) {
		JSONObject result = new JSONObject();
		result.put("action", object.getString("action"));
		List<RoomInfo> rooms=DataPool.getInstance().getRoomInfos();
		RoomInfo room = null;
		TableInfo table = null;
		for (RoomInfo roomInfo : rooms) {
			if (object.getString("roomId").equals(roomInfo.getRoomId())) {
				room = roomInfo;
				for (TableInfo tableInfo : room.getTables()) {
					if (tableInfo.getTableId().equals(object.getString("tableId"))) {
						table = tableInfo;
					}
				}
				break;
			}
		}
		PlayerInfo player = Casino28App.connector.getPlayerInfoBySocket(socket);
		String position = object.getString("pos");
		switch (position) {
		case "door1":		
			long wager=object.getLong("wager");
			
			table.getDoor1Players().add(player.getName());
			table.getDoor1Wagers().add(object.getLong("wager"));
		
			break;
		case "door2":
			table.getDoor2Players().add(player.getName());
			table.getDoor2Wagers().add(object.getLong("wager"));
			
			break;
		case "door3":
			table.getDoor3Players().add(player.getName());
			table.getDoor3Wagers().add(object.getLong("wager"));
			
			break;
		default:
			break;
		}

		DataPool.getInstance().getRoomInfos().get(Integer.parseInt(object.getString("roomId"))).getTables().set(Integer.parseInt(table.getTableId()), table);
		return result.put("state", 0).toString();
	}

}
